import { Navigate, useLocation } from 'umi';
import { Routes } from '@/routes';
import authorizationUtil from '@/utils/authorization-util';

const AuthGuard = ({ children }: { children: React.ReactNode }) => {
  console.log('AuthLayout1111');
  const location = useLocation();
  const isAuthenticated = authorizationUtil.getToken();
  console.log('isAuthenticated', isAuthenticated);

  if (!isAuthenticated && location.pathname !== Routes.Login) {
    return <Navigate to={Routes.Login} replace />;
  }
  if (isAuthenticated && location.pathname === Routes.Login) {
    return <Navigate to="/talk" replace />;
  }
  console.log('isAuthenticated', isAuthenticated, children);
  return <>{children}</>;
};

export default AuthGuard;